//
//  MineRainViewController.m
//  MJApp
//
//  Created by wangliang on 15/3/30.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineRainViewController.h"
#import "RainTableViewCell.h"
#import "DynamicSendView.h"
#import "LoginAndRegisterViewController.h"

@interface MineRainViewController ()<UITableViewDataSource,UITableViewDelegate,HomeCellDelegate>
@property (weak, nonatomic) IBOutlet UIButton *acountBtn;
@property (weak, nonatomic) IBOutlet UIButton *goodBtn;
@property (nonatomic, strong) UIButton *lastBtn;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *downBackView;
@property (weak, nonatomic) IBOutlet UIView *searchBackView;
@property (strong, nonatomic) DynamicSendView *sendView;
@property (nonatomic,strong) NSDictionary *infoDic;
@property (nonatomic,strong) NSMutableArray *discussArray;

@property (nonatomic,strong) NSDictionary *replyDic;

@end

@implementation MineRainViewController

- (NSMutableArray *)discussArray {
    
    if (!_discussArray) {
        
        _discussArray = [NSMutableArray array];
    }
    return _discussArray;
}

- (instancetype)initWithDic:(NSDictionary *)dic {
    
    if (self = [super init]) {
     
        self.infoDic = dic;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self UI];
    [self obationData];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
//    [((TabBarController *)self.tabBarController) setTabbarHidden:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)UI {
    
    self.title = @"评论";
    self.view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [self.goodBtn setTitle:@"赞（888）" forState:UIControlStateNormal];
    [self setButton:YES withBtn:self.acountBtn];
    [self setButton:NO withBtn:self.goodBtn];
    self.lastBtn = self.acountBtn;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    self.downBackView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    self.searchBackView.layer.cornerRadius = CGRectGetHeight(self.searchBackView.frame) / 2;
}

- (void)setButton:(BOOL)status withBtn:(UIButton *)button {
    
    if (status) {
        
        button.backgroundColor = [UIColor whiteColor];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }else {
        
        button.backgroundColor = [UIColor lightGrayColor];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
}

- (IBAction)headButtonAction:(id)sender {

    UIButton *btn = sender;
    if (btn != self.lastBtn) {
        
        [self setButton:YES withBtn:btn];
        [self setButton:NO withBtn:self.lastBtn];
        self.lastBtn = btn;
    }
}

- (IBAction)writeWord:(id)sender {
    
    if(![userInfoTool share].ID) {
        
        UINavigationController *loginNaviVC = [[UINavigationController alloc] initWithRootViewController:[[LoginViewController alloc] init]];
        [self presentViewController:loginNaviVC animated:YES completion:nil];
        return;
    }
    if (self.sendView == nil) {
        
        self.sendView = [[DynamicSendView alloc] initWithFrame:CGRectZero];
        [self.tabBarController.view addSubview:self.sendView];
        self.sendView.delegate = self;
        self.sendView.headTitle = @"写评论";
        [self.sendView resetView:NO];
    }else {
        
        self.sendView.hidden = NO;
        [self.sendView resetView:NO];
    }

}
//实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    //kbSize即為鍵盤尺寸 (有width, height)
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
    CGFloat kbHigh = kbSize.height;
    CGRect frame = self.sendView.backView.frame;
    frame.origin.y = CGRectGetHeight(self.sendView.frame) - kbHigh - CGRectGetHeight(self.sendView.backView.frame);
    self.sendView.backView.frame = frame;
}

#pragma mark tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
   
    return self.discussArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RainTableViewCell *cell = (RainTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHigh;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RainTableViewCell *cell = [RainTableViewCell initWithTableView:tableView];
    cell.infoDic = self.discussArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.replyDic = self.discussArray[indexPath.row];
    [self writeWord:nil];
}

- (void)homeCell:(id)cell withInfo:(id)info {
    
    if ([info[@"status"] isEqual:@"send"]) {
        
        
//        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//        dic[@"to_user_id"] =
        NSDictionary *dic = @{
        @"to_user_id":self.infoDic[@"user_id"],
        @"from_user_id":[userInfoTool share].ID,
        @"tid":self.infoDic[@"id"],
        @"type":@"0",
        @"content":self.sendView.textView.text
        };
        [WLHttpRequestAfter userReplayTrendWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
            
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:resultDictionary[@"msg"]];
            if ([resultDictionary[@"status"] integerValue] == 1) {
                
                [self obationData];
    
            }
            
        } andFailureBlock:^(NSError *resultError) {
            
        }];
    }
}

- (void)obationData {
    
    NSDictionary *dic = @{@"id":self.infoDic[@"id"]};
    [WLHttpRequestAfter userGetrendDiscussWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            [self.discussArray removeAllObjects];
            [self.discussArray addObjectsFromArray:resultDictionary[@"data"]];
            NSInteger rainNum = [resultDictionary[@"data"] count];
            NSString *rainStr = [NSString stringWithFormat:@"评价（%ld）",rainNum];
            [self.acountBtn setTitle:rainStr forState:UIControlStateNormal];
            [self.tableView reloadData];
        }
        
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

@end
